/**
*      author: Y_Yao
*      created: 2025-02-08 17:53:07
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 100 + 7;
int a[N][N][N],sum[N][N][N];
int n;

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            for(int k = 1;k <= n;k++)
            {
                cin >> sum[i][j][k];
            }
        }
    }

    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            for(int k = 1;k <= n;k++)
            {
                sum[i][j][k] += sum[i-1][j][k];
            }
        }
    }

    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            for(int k = 1;k <= n;k++)
            {
                sum[i][j][k] += sum[i][j-1][k];
            }
        }
    }

    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            for(int k = 1;k <= n;k++)
            {
                sum[i][j][k] += sum[i][j][k-1];
            }
        }
    }

    int q;
    cin >> q;
    while(q--)
    {
        int lx,rx,ly,ry,lz,rz;
        cin >> lx >> rx >> ly >> ry >> lz >> rz;
        int ans = sum[rx][ry][rz]
        - sum[lx-1][ry][rz] - sum[rx][ly-1][rz] - sum[rx][ry][lz-1]
        + sum[lx-1][ly-1][rz] + sum[lx-1][ry][lz-1] + sum[rx][ly-1][lz-1]
        - sum[lx-1][ly-1][lz-1];
        cout << ans << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}